package com.tao.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.base.exception.LearnHubException;
import com.tao.content.domain.pojo.CoursePublish;
import com.tao.learning.feignclient.ContentServiceClient;
import com.tao.learning.mapper.ChooseCourseMapper;
import com.tao.learning.mapper.CourseTablesMapper;
import com.tao.learning.model.dto.ChooseCourseDto;
import com.tao.learning.model.dto.CourseTablesDto;
import com.tao.learning.model.po.ChooseCourse;
import com.tao.learning.model.po.CourseTables;
import com.tao.learning.service.ICourseTables;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author taohongrun
 * @since 2024/11/28
 */
public class CourseTablesService extends ServiceImpl<CourseTablesMapper, CourseTables> implements ICourseTables {
    @Autowired
    ContentServiceClient contentServiceClient;
    @Autowired
    ChooseCourseMapper chooseCourseMapper;
    @Autowired
    CourseTablesMapper courseTablesMapper;
    public boolean saveChooseCourseStauts(String id){
        return true;
    }

    public CourseTablesDto getLearningStatus(String userId, Long courseId){
        //查询我的课程表
        CourseTables courseTables = getCourseTables(userId, courseId);
        if(courseTables==null){
            CourseTablesDto xcCourseTablesDto = new CourseTablesDto();
            //没有选课或选课后没有支付
            xcCourseTablesDto.setLearnStatus("702002");
            return xcCourseTablesDto;
        }
        CourseTablesDto courseTablesDto = new CourseTablesDto();
        BeanUtils.copyProperties(courseTables,courseTablesDto);
        //是否过期,true过期，false未过期
        boolean isExpires = courseTables.getValidtimeEnd().isBefore(LocalDateTime.now());
        if(!isExpires){
            //正常学习
            courseTablesDto.setLearnStatus("702001");
            return courseTablesDto;

        }else{
            //已过期
            courseTablesDto.setLearnStatus("702003");
            return courseTablesDto;
        }

    }

    @Transactional
    public ChooseCourseDto addChooseCourse(String userId, Long courseId) {
        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        //课程收费标准
        String charge = coursepublish.getCharge();
        //选课记录
        ChooseCourse chooseCourse = null;
        if ("201000".equals(charge)) {//课程免费
            //添加免费课程
            chooseCourse = addFreeCoruse(userId, coursepublish);
            //添加到我的课程表
            CourseTables xcCourseTables = addCourseTabls(chooseCourse);
        } else {
            //添加收费课程
            chooseCourse = addChargeCoruse(userId, coursepublish);
        }
        ChooseCourseDto xcChooseCourseDto = new ChooseCourseDto();
        BeanUtils.copyProperties(chooseCourse,xcChooseCourseDto);
        //获取学习资格
        CourseTablesDto xcCourseTablesDto = getLearningStatus(userId, courseId);
        xcChooseCourseDto.setLearnStatus(xcCourseTablesDto.getLearnStatus());
        return xcChooseCourseDto;

    }




    //添加免费课程,免费课程加入选课记录表、我的课程表
    public ChooseCourse addFreeCoruse(String userId, CoursePublish coursepublish) {
        //查询选课记录表是否存在免费的且选课成功的订单
        LambdaQueryWrapper<ChooseCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(ChooseCourse::getUserId, userId)
                .eq(ChooseCourse::getCourseId, coursepublish.getId())
                .eq(ChooseCourse::getOrderType, "700001")//免费课程
                .eq(ChooseCourse::getStatus, "701001");//选课成功
        List<ChooseCourse> xcChooseCourses = chooseCourseMapper.selectList(queryWrapper);
        if (xcChooseCourses != null && xcChooseCourses.size()>0) {
            return xcChooseCourses.get(0);
        }
        //添加选课记录信息
        ChooseCourse xcChooseCourse = new ChooseCourse();
        xcChooseCourse.setCourseId(coursepublish.getId());
        xcChooseCourse.setCourseName(coursepublish.getName());
        xcChooseCourse.setCoursePrice(0f);//免费课程价格为0
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setCompanyId(coursepublish.getCompanyId());
        xcChooseCourse.setOrderType("700001");//免费课程
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setStatus("701001");//选课成功

        xcChooseCourse.setValidDays(365);//免费课程默认365
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(365));
        chooseCourseMapper.insert(xcChooseCourse);

        return xcChooseCourse;

    }

    //添加收费课程
    public ChooseCourse addChargeCoruse(String userId,CoursePublish coursepublish){
        //如果存在待支付交易记录直接返回
        LambdaQueryWrapper<ChooseCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(ChooseCourse::getUserId, userId)
                .eq(ChooseCourse::getCourseId, coursepublish.getId())
                .eq(ChooseCourse::getOrderType, "700002")//收费订单
                .eq(ChooseCourse::getStatus, "701002");//待支付
        List<ChooseCourse> xcChooseCourses = chooseCourseMapper.selectList(queryWrapper);
        if (xcChooseCourses != null && xcChooseCourses.size()>0) {
            return xcChooseCourses.get(0);
        }

        ChooseCourse xcChooseCourse = new ChooseCourse();
        xcChooseCourse.setCourseId(coursepublish.getId());
        xcChooseCourse.setCourseName(coursepublish.getName());
        xcChooseCourse.setCoursePrice(coursepublish.getPrice());
        xcChooseCourse.setUserId(userId);
        xcChooseCourse.setCompanyId(coursepublish.getCompanyId());
        xcChooseCourse.setOrderType("700002");//收费课程
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setStatus("701002");//待支付

        xcChooseCourse.setValidDays(coursepublish.getValidDays());
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(coursepublish.getValidDays()));
        chooseCourseMapper.insert(xcChooseCourse);
        return xcChooseCourse;

    }

    //添加到我的课程表
    public CourseTables addCourseTabls(ChooseCourse xcChooseCourse){
        //选课记录完成且未过期可以添加课程到课程表
        String status = xcChooseCourse.getStatus();
        if (!"701001".equals(status)){
            LearnHubException.throwMyException("选课未成功，无法添加到课程表");
        }
        //查询我的课程表
        CourseTables xcCourseTables = getCourseTables(xcChooseCourse.getUserId(), xcChooseCourse.getCourseId());
        if(xcCourseTables!=null){
            return xcCourseTables;
        }
        CourseTables xcCourseTablesNew = new CourseTables();
        xcCourseTablesNew.setChooseCourseId(xcChooseCourse.getId());
        xcCourseTablesNew.setUserId(xcChooseCourse.getUserId());
        xcCourseTablesNew.setCourseId(xcChooseCourse.getCourseId());
        xcCourseTablesNew.setCompanyId(xcChooseCourse.getCompanyId());
        xcCourseTablesNew.setCourseName(xcChooseCourse.getCourseName());
        xcCourseTablesNew.setCreateDate(LocalDateTime.now());
        xcCourseTablesNew.setValidtimeStart(xcChooseCourse.getValidtimeStart());
        xcCourseTablesNew.setValidtimeEnd(xcChooseCourse.getValidtimeEnd());
        xcCourseTablesNew.setCourseType(xcChooseCourse.getOrderType());
        courseTablesMapper.insert(xcCourseTablesNew);

        return xcCourseTablesNew;

    }


    public CourseTables getCourseTables(String userId,Long courseId){
        return courseTablesMapper.selectOne(new LambdaQueryWrapper<CourseTables>().eq(CourseTables::getUserId, userId).eq(CourseTables::getCourseId, courseId));
    }


}
